package b2.widget.tech
{
	import b2.config.*;
	import b2.net.*;
	import b2.player.*;
	import b2.util.*;
	import b2.widget.*;
	import b2.widget.tab.*;
	import b2.widget.tank.*;
	import b2.widget.warehouse.*;
	
	import com.greensock.TweenLite;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;
	import flash.utils.Timer;
	
	public class TechTower extends TabController
	{
		private static var instance:TechTower;
		//是否显示帮助
		public var guideFlag:Boolean;
		//帮助容器
		private var guideContainter:Sprite;
		//帮助步骤
		private var guideStep:int;
		//计时器
		private var timer:Timer;
		
		public function TechTower()
		{
			var TabControllerBackground:Class = ResourceHelper.getInstance().getClassByName("TechTowerDialogBgr");
			var TabTitleBackground:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr6");
			var HighlightTitleBgr:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr3");
			var CloseButton:Class = ResourceHelper.getInstance().getClassByName("CloseButton");
			super(TabControllerBackground);
			//标题
			var titleText:TextField = new TextField();
			titleText.selectable = false;
        	titleText.autoSize = TextFieldAutoSize.CENTER;
        	var titleFomat:TextFormat = new TextFormat("微软雅黑", 16, 0xFFFFFF);
        	titleFomat.letterSpacing = 10;
			titleText.defaultTextFormat = titleFomat;
			titleText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			titleText.text = "科技炉";
			titleText.x = (this.width - titleText.width) / 2;
			titleText.y = 13;
			addChild(titleText);
			var closeButton:BasicButton = new BasicButton(CloseButton);
			closeButton.x = this.width - 42;
			closeButton.y = 12;
			addChild(closeButton);
			closeButton.addEventListener(MouseEvent.CLICK, onCloseButtonClick);
			var strengthenTab:StrengthenTab = new StrengthenTab();
			this.addTab(TabTitleBackground, "强化", strengthenTab, HighlightTitleBgr);
			var fuseTab:FuseTab = new FuseTab();
			this.addTab(TabTitleBackground, "合成", fuseTab, HighlightTitleBgr);
			this.setTabTitlePos(25, 48);
			this.setTabSelectCallBack(onTabSelectChange);
			this.guideStep = 4;
			var mixtureTab:MixtureTab = new MixtureTab();
			this.addTab(TabTitleBackground, "熔炼", mixtureTab, HighlightTitleBgr);
			this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			this.addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
		}
		
		private function onAddedToStage(event:Event):void
		{
			if (guideFlag)
			{
				guideStep = 0;
				timer = new Timer(5000);
				timer.addEventListener(TimerEvent.TIMER, onGuideTick);
				timer.start();
			}
		}

		private function onRemovedFromStage(event:Event):void
		{
        	if (timer != null)
        	{
				timer.stop();
				timer.removeEventListener(TimerEvent.TIMER, onGuideTick);
        	}
			timer = null;
			if (guideContainter != null && guideContainter.parent != null)
				guideContainter.parent.removeChild(guideContainter);
		}
		
		
		public static function getInstance():TechTower
		{
			if (!instance)
				instance = new TechTower();
			return instance;
		}
		
		public function removeAll():void
		{
			for (var i:int = 0; i < tabs.length; i++)
			{
				tabs[i].removeAll();
			}
		}
		
		public function close():void
		{
			if (this.parent != null)
			{
				removeAll();
				this.parent.removeChild(this);
			}
		}
		
		private function onGuideTick(event:Event):void
		{
			if (guideContainter != null && guideContainter.parent != null)
				guideContainter.parent.removeChild(guideContainter);
			guideContainter = new Sprite();
			addChild(guideContainter);
			var blinkRect:BlinkRec;
			var bubbleMessage:BubbleMessage;
			if (guideStep == 0)
			{
				blinkRect = new BlinkRec(373, 162, 328, 249);
				bubbleMessage = new BubbleMessage("双击需要强化/合成的物品");
				bubbleMessage.x = 378;
				bubbleMessage.y = 91;
			}
			else if (guideStep == 1)
			{
				blinkRect = new BlinkRec(20, 95, 310, 78);
				bubbleMessage = new BubbleMessage("鼠标悬浮查看强化/合成前后的属性变化", true);
				bubbleMessage.x = 250;
				bubbleMessage.y = 4;
			}
			else if (guideStep == 2)
			{
				blinkRect = new BlinkRec(22, 234, 140, 185);
				bubbleMessage = new BubbleMessage("这里显示强化/合成所消耗的材料");
				bubbleMessage.x = 20;
				bubbleMessage.y = 163;
			}
			else if (guideStep == 3)
			{
				blinkRect = new BlinkRec(190, 236, 140, 135);
				bubbleMessage = new BubbleMessage("增加催化剂能够增加成功率");
				bubbleMessage.x = 190;
				bubbleMessage.y = 167;
			}
			else
			{
				blinkRect = new BlinkRec(219, 370, 92, 38);
				bubbleMessage = new BubbleMessage("点击立即开始强化/合成", true);
				bubbleMessage.x = 322;
				bubbleMessage.y = 316;
			}
			guideContainter.addChild(blinkRect);
			guideContainter.addChild(bubbleMessage);
			guideStep = (guideStep + 1) % 5;
		}
		
		private function onTabSelectChange():void
		{
			removeAll();
			Warehouse.getInstance().refresh();
		}
		
		private function onCloseButtonClick(event:MouseEvent):void
		{
			close();
			Warehouse.getInstance().close();
		}
	}
}